package cn.chenxinjie.hrm.authenhandle;

import cn.chenxinjie.hrm.domain.Permission;
import cn.chenxinjie.hrm.mapper.PermissionMapper;
import cn.chenxinjie.hrm.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;

/**
 * @author 陈鑫杰
 * @date 2022/3/19 14:44
 */
@Component
public class MyAuthenSuccess implements AuthenticationSuccessHandler {


    @Override
    public void onAuthenticationSuccess(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Authentication authentication) throws IOException, ServletException {
        Collection<? extends GrantedAuthority> authorities = authentication.getAuthorities();
        authorities.stream().forEach(e -> {
            System.out.println("具有的权限有:" + e.getAuthority());
        });
        AjaxResult result = AjaxResult.me().setMessage("登陆成功:" + authentication.getName());
        httpServletResponse.setContentType("text/json;charset=utf-8");
        PrintWriter writer = httpServletResponse.getWriter();
        writer.write(result.toString());
    }
}
